#include "application.h"

int application::runApp()
{
	int carType;
	cars *car;
	client *c1;
	string carName, carColor, carTransmission, carDimensions, carEngine, carEquipment;
	string clientName, clientSurname;
	int clientAge;
	int menu = 1;
	vector<client*> clientDatabase;
	int i,j;

	while (menu)
	{
		cout << "**********************************************************" << endl;
		cout << "***********************Car acquire************************" << endl;
		cout << "1. Insert a new car" << endl;
		cout << "2. Delete a car" << endl;
		cout << "3. Exit & show results" << endl;
		cout << "Option: ";
		cin >> menu;
		switch (menu)
		{
		case 1:
			{	
				cout << "**********************************************************" << endl;
				cout << "*************************New car**************************" << endl;
				cout << "Insert car type:" << endl << "1. Sedan" << endl << "2. Coupe" << endl << "3. Mini" << endl;
				cout << "Car type: ";
				cin >> carType;
				switch (carType)
				{
				case 1:
					{
						car = new sedan();
						break;
					}
				case 2:
					{
						car = new coupe();
						break;
					}
				case 3:
					{
						car = new mini();
						break;
					}
				default:
					{
						cout << "Optiune inexistenta!" << endl;
						return 0;
					}
				}
				cout << "Insert car specifications:" << endl;
				cout << "Car name: ";
				cin >> carName;
				cout << "Car color: ";
				cin >> carColor;
				cout << "Car transmission: ";
				cin >> carTransmission;
				cout << "Car dimensions: ";
				cin >> carDimensions;
				cout << "Car engine: ";
				cin >> carEngine;
				cout << "Car equipement: ";
				cin >> carEquipment;
				// set car attributes
				car -> setCarName(carName);
				car -> setColor(carColor);
				car -> setDimensions(carDimensions);
				car -> setTransmission(carTransmission);
				car -> setEngine(carEngine);
				car -> setEquipment(carEquipment);
				// insert a new client
				cout << "**********************************************************" << endl;
				cout << "***********************Client*************************" << endl;
				cout << "Insert client specifications:" << endl;
				cout << "Name: ";
				cin >> clientName;
				cout << "Surname: ";
				cin >> clientSurname;
				cout << "Age: ";
				cin >> clientAge;

				if ((int)clientDatabase.size() == 0)
				{
					c1 = new client();
					// set client attributes
					c1 -> setName(clientName);
					c1 -> setSurname(clientSurname);
					c1 -> setAge(clientAge);
					c1 -> setCarList(car);
					clientDatabase.push_back(c1);
				}
				else
				{
					int foundClient, foundClientPos;
					foundClient = 0;
					foundClientPos = 0;
					for (i=0; i<(int)clientDatabase.size(); i++)
					{
						if ((clientDatabase.at(i)->getName() == clientName) && (clientDatabase.at(i)->getSurname() == clientSurname) && (clientDatabase.at(i)->getAge() == clientAge))
						{
							foundClient = 1;
							foundClientPos = i;
						}
					}
					if (foundClient == 1)
					{
						clientDatabase.at(foundClientPos) -> setCarList(car);
					}
					else
					{
						c1 = new client();
						// set client attributes
						c1 -> setName(clientName);
						c1 -> setSurname(clientSurname);
						c1 -> setAge(clientAge);
						c1 -> setCarList(car);
						clientDatabase.push_back(c1);
					}
				}
				break;
			}
		case 2:
			{
				cout << "Insert client specifications:" << endl;
				cout << "Name: ";
				cin >> clientName;
				cout << "Surname: ";
				cin >> clientSurname;
				cout << "Age: ";
				cin >> clientAge;

				
				if ((int)clientDatabase.size() == 0)
				{
					cout << "There are no clients in our database!" << endl;
				}
				else
				{
					int foundClient, foundClientPos;
					foundClient = 0;
					foundClientPos = 0;
					for (i=0; i<(int)clientDatabase.size(); i++)
					{
						if ((clientDatabase.at(i)->getName() == clientName) && (clientDatabase.at(i)->getSurname() == clientSurname) && (clientDatabase.at(i)->getAge() == clientAge))
						{
							foundClient = 1;
							foundClientPos = i;
						}
					}
					if (foundClient == 1)
					{
						string delCarName;
						cout << "Insert car name: ";
						cin >> delCarName;
						for (j=0; j<(int)clientDatabase.at(foundClientPos)->getCarList().size(); j++)
						{
							if (clientDatabase.at(foundClientPos)->getCarList().at(j)->getCarName() == delCarName)
							{
								// next function should work. i couldn't find the problem :(
								// clientDatabase.at(foundClientPos)->getCarList().erase(clientDatabase.at(foundClientPos)->getCarList().begin()+j);
								cout << "Delete car" << endl;
							}
						}
					}
					else
					{
						cout << "There is no " << clientSurname << " " << clientName << " in our database!" << endl;
					}
				}
				break;
			}
		case 3:
			{
				cout << "Goodbye!" << endl;
				menu = 0;
				break;
			}
		default:
			{
				cout << "Optiune inexistenta!" << endl;
				break;
			}
		}
	}

	cout << "**********************************************************" << endl;
	cout << "***********************Results****************************" << endl;
	if ((int)clientDatabase.size() == 0)
	{
		cout << "No client inserted!" << endl;
		return 0;
	}
	else
	{
		for (j=0; j<(int)clientDatabase.size(); j++)
		{
			cout << "Client: " << clientDatabase.at(j)->getSurname() << " " << clientDatabase.at(j)->getName() << ", age " << clientDatabase.at(j)->getAge() << endl;
			for (int i=0; i<(int)clientDatabase.at(j)->getCarList().size() ;i++)
				cout << "Car type: " << clientDatabase.at(j) -> getCarList().at(i) -> carType() << endl
					<< "Car name: " << clientDatabase.at(j) -> getCarList().at(i) -> getCarName() << endl
					<< "Car color: " << clientDatabase.at(j) -> getCarList().at(i) -> getColor() << endl
					<< "Car dimensions: " << clientDatabase.at(j) -> getCarList().at(i) -> getDimensions() << endl
					<< "Car engine: " << clientDatabase.at(j) -> getCarList().at(i) -> getEngine() << endl
					<< "Car equipment: " << clientDatabase.at(j) -> getCarList().at(i) -> getEquipment() << endl
					<< "Car transmission: " << clientDatabase.at(j) -> getCarList().at(i) -> getTransmission() << endl << endl;
		}
	}
	return 0;
}